package com.atguigu.tingshu.order.receiver;

import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.order.service.MqOpsService;
import com.rabbitmq.client.Channel;
import io.micrometer.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author：yyj
 * @Package：com.atguigu.tingshu.order.receiver
 * @Project：tingshu_parent240829
 * @name：OrderInfoReceiver
 * @Date：2025/2/13 10:15
 * @Filename：OrderInfoReceiver
 */
@Component
@Slf4j
public class OrderInfoReceiver {
    @Autowired
    private MqOpsService mqOpsService;
    @RabbitListener(bindings=@QueueBinding(value = @Queue(value = MqConst.QUEUE_LOCAL_MSG, durable = "true", exclusive = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_LOCAL_MSG),
            key = MqConst.ROUTING_LOCAL_MSG))
    public void listenLocalMsg(String content, Message message, Channel channel) {
        // 1.获取消息，进行判断
        if (StringUtils.isEmpty(content)) {
            return;
        }
        //消费消息
        mqOpsService.updateLocalMsg(content);
        //手动应答
        try {
            long deliveryTag = message.getMessageProperties().getDeliveryTag();
            channel.basicAck(deliveryTag,false);
        } catch (Exception e) {
            log.error("消息消费失败：{}",e.getMessage());
        }
    }

    @RabbitListener(queues = MqConst.QUEUE_CANCEL_ORDER)
    public void cancelOrder(String content, Message message, Channel channel) {
        // 1.获取消息，进行判断
        if (StringUtils.isEmpty(content)) {
            return;
        }
        //消费消息
        mqOpsService.cancelOrder(content);
        log.info("关闭订单完成操作");

        //手动应答
        try {
            long deliveryTag = message.getMessageProperties().getDeliveryTag();
            channel.basicAck(deliveryTag,false);
        } catch (Exception e) {
            log.error("消息消费失败：{}",e.getMessage());
        }
    }
}